#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <stdlib.h>
//#include <linux/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <fcntl.h>

#include "config.h"

char *getdir(char *cmd)
{
	char *p;
	p = strtok(cmd," ");
	p = strtok(NULL," ");
	
	return p;
}

int get_cmd_type(char *cmd)
{
	
	if(strstr(cmd,"lcd"))   return LCD;


	if(!strcmp("quit",cmd))   return QUIT;
	if(!strcmp("ls",cmd))    return LS;
	if(!strcmp("lls",cmd))   return LLS;
	if(!strcmp("pwd",cmd))   return LS;

	if(strstr(cmd,"cd"))    return CD;
	if(strstr(cmd,"get"))   return GET;
	if(strstr(cmd,"put"))   return PUT;
        return -1;

}

int cmd_handler(struct Msg msg,int fd)
{
	char *dir = NULL;
	char buf[32];
	int ret;
	int filefd;
	
	ret = get_cmd_type(msg.data);
	
	switch(ret)
	{
		case LS:
		case CD:
		case PWD:
			msg.type = 0;
			write(fd,&msg,sizeof(msg));
			break;
		case GET:
			msg.type = 2;
			write(fd,&msg,sizeof(msg));
			break;
		case PUT:
			strcpy(buf,msg.data);
			dir = getdir(buf);
			
			if(access(dir,F_OK) == -1)
			{
				printf("%s is not exist\n",dir);
			}else{
				filefd = open(dir,O_RDWR);
				read(filefd,msg.secondBuf,sizeof(msg.secondBuf));
				close(filefd);
				write(fd,&msg,sizeof(msg));
			}
			break;
		case LLS:
			system("ls");
			break;
		case LCD:
			dir = getdir(msg.data);
			chdir(dir);
			break;
		case QUIT:
			strcpy(msg.data,"quit");
			write(fd,&msg,sizeof(msg));
			close(fd);
			exit(-1);
	}
	return ret;
}

void handler_server_msg(struct Msg msg,int c_fd)
{
	int n_read;
	struct Msg msgget;
	int newfilefd;
	
 	n_read = read(c_fd,&msgget,sizeof(msgget));
	
	if(n_read == 0)
	{
		printf("server is out or no exist!\n");
		exit(-1);
	}
	 else if(msgget.type ==DOFILE)
	{
		char *p = getdir(msg.data);
		newfilefd = open(p,O_RDWR|O_CREAT,0760);
		write(newfilefd,msgget.data,strlen(msgget.data));
		putchar('>');
		fflush(stdout);
	}
	else{
		printf("***********************************\n");
		printf("\n%s\n",msgget.data);
		printf("***********************************\n");
		putchar('>');
		fflush(stdout);
	}
	
}
int main(int argc,char **argv)
{
	int c_fd;
	struct sockaddr_in c_addr;
	
	struct Msg msg;	
	
	if(argc !=3)
	{
		printf("param is error\n");
		exit(-1);
	}

	memset(&c_addr,0,sizeof(struct sockaddr_in));

	//1.socket
	 c_fd = socket(AF_INET,SOCK_STREAM,0); 
	if(c_fd == -1)
	{
		perror("Socket");
		exit(-1);
	} 	
	c_addr.sin_family = AF_INET;
	c_addr.sin_port = htons(atoi(argv[2])); //zi jie xu wen ti
	inet_aton(argv[1],&c_addr.sin_addr);
	
	//2.connect
	if(connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr)) == -1)
	{
		perror("Connect");	
		exit(-1);
	}
	printf("connect ...\n");
	int mark = 0;
	//write cmd and read ftp_server info
	while(1){
	memset(msg.data,0,sizeof(msg.data));
	if(mark == 0) printf("input >");
	gets(msg.data);
	if(strlen(msg.data) == 0)
	{
		if(mark == 1) putchar('>');
		continue;
	}
	mark = 1;
	//to switch do cmd_handler	
	int ret = cmd_handler(msg,c_fd);

	if(ret>IFGO){
		putchar('>');
		fflush(stdout);
		continue;
	}	
	if(ret == -1)
	{
		printf("cmd not find\n");
		putchar('>');
		fflush(stdout);
		continue;
	}
	//to read server return  info
	handler_server_msg(msg,c_fd);

	
	
		}
	return 0;
}
